﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Core.Data.Customers;
using Shop.Core.Domain.Customers;
using Shop.Service.Resources;
using Shop.Infrastructure.Extensions;
using Shop.Service.Security;
using Shop.Infrastructure.Caching;
using Shop.Infrastructure.MangoDB;
using Shop.Infrastructure.Exceptions;

namespace Shop.Service.Customers
{
    public class CustomerService : ICustomerService
    {
        public ICustomerRepository CustomerRepository
        {
            get;
            protected set;
        }

        public IEncryptionService EncryptionService
        {
            get;
            protected set;
        }

        public CustomerService(ICustomerRepository customerRepository, IEncryptionService encryptionService)
        {
            this.CustomerRepository = customerRepository;
            this.EncryptionService = encryptionService;
        }

        public Core.Domain.Customers.Customer GetCustomer(int id)
        {
            return this.CustomerRepository.GetById(id);
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns>创建成功的用户对象</returns>
        /// <exception cref="System.InvalidOperationException, System"/>
        public Customer CreateCustomer(CreateCustomerRequest request)
        {
            Customer existCustomer = this.GetCustomerByEmail(request.Email);
            if (existCustomer != null)
            {
                throw new MessageException(CustomerResource.CustomerExisted)
                    .AddContext(ErrorCode.Unknown, new
                    {
                        Email = request.Email
                    });
            }
            existCustomer = this.GetCustomerByNickName(request.NickName);
            if (existCustomer != null)
            {
                throw new MessageException(CustomerResource.CustomerExisted)
                    .AddContext(ErrorCode.Unknown, new
                    {
                        NickName = request.NickName
                    });
            }
            string password = this.EncryptionService.EncryptText(request.Password);//加密
            Customer customer = CustomerFactory.CreateCustomer(request.Email, request.NickName
                , password);
            this.CustomerRepository.Insert(customer);
            return customer;
        }

        public IList<Core.Domain.Customers.Customer> FindCustomerById(IList<int> idList)
        {
            return this.CustomerRepository.FindByIdList(idList);
        }

        public Customer GetCustomerByEmail(string email)
        {
            return this.CustomerRepository.GetCustomerByEmail(email);
        }

        public Customer GetCustomerByNickName(string nickName)
        {
            return this.CustomerRepository.GetCustomerByNickName(nickName);
        }

        /// <summary>
        /// Insert a guest customer
        /// </summary>
        /// <returns>Customer</returns>
        public virtual Customer CreateGuestCustomer()
        {
            Customer customer = CustomerFactory.CreateGuestCustomer();
            MangoDBClient.Save(customer.NickName, customer);
            return customer;
        }

        public virtual Customer GetGuestCustomer(string nickName)
        {
            return MangoDBClient.Get<Customer>(nickName);
        }
    }
}
